<template>
  <!-- 借进入库 -->
  <div>
    <FilterTable
      ref="table"
      :columns="columns"
      columnsRef="part_report_reportInventorytable_spa"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectHandle"
      selectionType="single"
      :isExportExcel="true"
      :onCalcExportData="exportFun"
      exportFileName="配件综合情况查询-入库单.xlsx"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"
    >
      <!-- :dataSource="dataSource"
      :isMemoryPagination="true" -->
      <!-- :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'" -->
      <template slot="controls">
        <!-- <AjaxButton size="small" type="primary" icon="el-icon-printer" :disabled="printFlag" :click="printHandle">打印</AjaxButton> -->
      </template>
      <template slot="controls">
        <AjaxButton size="small" type="primary" icon="el-icon-printer" :click="exportHandle">导出</AjaxButton>
      </template>
      </FilterTable
    >
    <div v-if="printBtn">
      <!-- <BasePrint ref="Salesprint" :data="SalesprintList" template="templatepartInventory" /> -->
      <!-- 原templatePartAdjustInventory -->
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { queryPartInOutStockReport, getAllUser, exportPartInOutStockReport } from '@/api/part/report/billSearch';
import { printData } from '@/api/part/partInventory/adjustInventory/index';
import { getStroageList } from '@/api/part/partOutbound/partBreak';
import { exportExcelFile } from '@/utils/index';

export default {
  name: 'reportInventorytable',
  mixins: [dictionary],
  props: {
    fetchParamObj: Object,
    type: String,
    userList: Array,
    tax: Number,
    userItems: Array
  },

  data() {
    return {
      fetchApi: queryPartInOutStockReport,
      columns: this.createdTable(),
      fetchParams: { xhrAbort: true },
      selectColumns: [],
      printFlag: false, // 打印按钮
      SalesprintList: {}, // 打印的数据
      printBtn: false, // 控制打印
      cunUserList: [], // 存所有人的数组
      printDataList: [], // 存打印接口返回的数据
      dataSource: []
    };
  },
  watch: {
    userList(val) {
      for (const key in val) {
        arr[{ text: val[key], value: key }];
      }
      // console.log(123, arr);
      this.columns.find(item => item.dataIndex == 'OPERATOR_NAME').filterItems = arr;
    },
    fetchParamObj(fetchParamObj) {
      // console.log(fetchParamObj, 'fetchParamObj----1');
      this.initTableData(fetchParamObj);
    }
  },
  mounted() {
    // this.getStorage();
    this.getEmployee();
  },
  methods: {
    // 导出方法
    exportFun(row) {
      for (const key in row) {
        switch (key) {
          // case 'OPERATE_DATE':
          //   if (row[key]) {
          //     row[key] = moment(row[key]).format('YYYY-MM-DD');
          //   }
          //   break;
          case 'OPERATOR_NAME':
            if (row[key]) {
              row[key] = this.userList[row[key]];
            }
            break;
          case 'STOCK_IN_QUANTITY':
          case 'IN_OUT_TAXED_PRICE':
          case 'COST_PRICE':
          case 'IN_OUT_TAXED_AMOUNT':
          case 'COST_AMOUNT':
            if (row[key]) {
              row[key] = Number(row[key]).toFixed(2);
            }
            break;
          default:
            break;
        }
      }
    },

    // 选中回调
    selectHandle(val) {
      this.selectColumns = val;
    },
    //导出
    async exportHandle(){
      let res = await exportPartInOutStockReport(this.fetchParams);
      console.log(res,"导出::");
      try{
        exportExcelFile(res.data,'入库单')
      }catch(e){
        console.log(e,"错误::");
      }
    },
    // 打印数据接口
    async getPrintData() {
      // console.log(this.selectColumns[0].PART_ALLOCATE_IN_ID);
      const res = await printData(this.selectColumns[0].PART_ALLOCATE_IN_ID); // 获取当前行数据
      res.data.detailInfo.map(item => {
        item.IN_AMOUNT_TAXED = item.IN_AMOUNT * (1 + this.tax * 1) || 0;
        item.IN_PRICE_TAXED = item.IN_PRICE * (1 + this.tax * 1) || 0;
      });
      this.printDataList = res.data;
    },
    // 打印处理
    async printHandle() {
      // 判断是否选中数据
      if (this.selectColumns.length < 1) {
        return this.$notify.error({ title: '提示信息', message: '请选中一条数据' });
      }
      await this.getPrintData();
      this.printBtn = true;

      this.SalesprintList = this.printDataList;
      this.SalesprintList.baseInfo.STOCK_IN_TYPE = '借进入库';
      // console.log(this.cunUserList, this.SalesprintList, this.printDataList);
      this.SalesprintList.baseInfo.USER_NAME = this.cunUserList[this.SalesprintList.baseInfo.USER_ID];
      // 打印
      this.$nextTick(() => {
        this.$refs.Salesprint.EXCUTE_PRINT();
      });
    },

    async initTableData(obj) {
      const fetchParamObj = this.fetchParamObj;
      this.fetchParams = Object.assign({}, fetchParamObj, { xhrAbort: false });
      // this.fetchParams = Object.assign({}, fetchParamObj);
      // try {
      //   this.$refs.table.START_LOADING();
      //   this.dataSource = [];
      //   let res = await queryPartInOutStockReport(this.fetchParams);
      //   this.dataSource = res.data;
      //   this.$refs.table.STOP_LOADING();
      // } catch (error) {
      //   console.log(error);
      // } finally {
      //   this.$refs.table.STOP_LOADING();
      // }
    },

    // 获取仓库
    async getStorage() {
      const res = await getStroageList();
      this.columns.find(x => x.dataIndex === 'STORAGE_CODE').editItems = res.data.map(x => ({ text: x.STORAGE_NAME, value: x.STORAGE_CODE + '' }));
    },
    // 操作员名称
    async getEmployee() {
      // 获取人员列表
      const res = await getAllUser();
      if (res.resultCode == 200) {
        res.data.map(e => {
          this.cunUserList[e.userId + ''] = e.employeeName;
        });
      }
      // this.columns.find(item => item.dataIndex === 'HANDLER').filterItems = res.data.map(x => ({ text: x.employeeName, value: x.userId + '' }));
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        // {
        //   title: '入库单编号',
        //   dataIndex: 'IN_NO',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        {
          title: '配件名称',
          dataIndex: 'PART_NAME',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '配件说明',
          dataIndex: 'PART_DESCRIPTION',
          sorter: true,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: 'CCC件类型',
          dataIndex: 'IS_CCC_PART',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '配件代码',
          dataIndex: 'PART_NO',
          sorter: true,
          filter: true,
          filterType: 'input',
          width: '160',
          showOverflowTooltip: true
        },
        {
          title: '原单编号',
          dataIndex: 'ORIGINAL_ORDER_NUMBER',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '对应单编号',
          dataIndex: 'SHEET_NO',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '入库方式',
          dataIndex: 'IN_OUT_TYPE',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('8249'),
          showOverflowTooltip: true,
          render: props => {
            return <span>{this.createDictText(props.row.IN_OUT_TYPE, '8249')}</span>;
          }
        },
        {
          title: '入库日期',
          dataIndex: 'OPERATE_DATE',
          // dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '入库数量',
          dataIndex: 'STOCK_IN_QUANTITY',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true,
          precision: 2,
          summation: true,
          summationDataIndex: 'TOTAL_STOCK_IN_QUANTITY'
        },
        {
          title: '供应商名称',
          dataIndex: 'SUPPLIER_NAME',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '仓库代码',
          dataIndex: 'STORAGE_CODE',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '税率(%)',
          dataIndex: 'tax',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '操作员代码',
          dataIndex: 'OPERATOR',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '操作员名称',
          dataIndex: 'OPERATOR_NAME',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true,
          render: props => {
            return <span>{this.userList[props.row.OPERATOR_NAME]}</span>;
          }
        },
        {
          title: 'ABC分类',
          dataIndex: 'ABC_TYPE',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('8092'),
          showOverflowTooltip: true,
          render: props => {
            return <span>{this.createDictText(props.row.ABC_TYPE, '8092')}</span>;
          }
        },
        {
          title: '保质期',
          dataIndex: 'SHELF_DAYS',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '计量单位',
          dataIndex: 'UNIT_NAME',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '含税采购单价',
          dataIndex: 'IN_OUT_TAXED_PRICE',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true,
          precision: 2
        },
        {
          title: '不含税成本单价',
          dataIndex: 'COST_PRICE',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true,
          precision: 2
        },
        {
          title: '含税入库金额',
          dataIndex: 'IN_OUT_TAXED_AMOUNT',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true,
          precision: 2,
          summation: true,
          summationDataIndex: 'TOTAL_IN_OUT_TAXED_AMOUNT'
        },
        {
          title: '不含税成本金额',
          dataIndex: 'COST_AMOUNT',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true,
          precision: 2,
          summation: true,
          summationDataIndex: 'TOTAL_COST_AMOUNT'
        },
        // {
        //   title: '库位编号',
        //   dataIndex: 'STORAGE_POSITION_CODE',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        {
          title: '主组号',
          dataIndex: 'MAIN_GROUP_NO',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        }
      ];
    }
  }
};
</script>

<style lang="less" scoped>
/* .form-group {
  margin-top: 10px;
  span {
    height: 50px;
    line-height: 50px;
    text-align: right;
    width: 250px;
    padding-left: 100px;
  }
} */
</style>
